<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.min.js"></script>
    <link rel="stylesheet" href="./maze-editor.css" />

</head>
<!-- https://leetcode-cn.com/problems/max-area-of-island/ -->
<body>
    <div id="app">
        <div id="container">
            <div v-for="(arrItem,arrItemIndex) in mazeData" :id="arrItemIndex" :key="arrItemIndex" class="row">
                <div v-for="(colItem,colIndex) in arrItem" :id="1+colIndex+4*arrItemIndex" :key="colIndex"
                    :class="['tile', 'tile-'+colItem]" :type="colItem">
                    <span class="toShow">{{colItem}}</span>
                </div>
            </div>
        </div>
        <button class="btn btn-teal" @click="maxAreaOfIsland">计算</button>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                mazeData: [[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]],
            },
            methods: {
                 maxAreaOfIsland () {
                     const grid=this.mazeData;
                    let rowCount = grid.length;
                    let colCount = grid[0].length;
                    let areaNumber = 10;
                    let obj = {};
                    let visited = new Array();

                    for (let i = 0; i < rowCount; i++) {
                        visited[i] = new Array();
                        for (let j = 0; j < colCount; j++) {
                            visited[i][j] = 0;
                        }
                    }
                    const dfs = function (x, y) {
                        console.log('x,y', x, y);
                        if (x < 0 || x >= rowCount || y < 0 || y >= colCount) {//过界直接返回
                            console.log('cross edge');
                            return;
                        }
                        if (grid[x][y] === 0) {//是水，直接返回
                            console.log('water');
                            return;
                        }
                        if (visited[x][y] === 1) {//访问过直接返回
                            console.log('visited');
                            return;
                        }
                        if (grid[x][y] > 1) {//标记过区域号，直接返回,区号(areaNumber)从10开始
                            console.log('marked before,other area ', grid[x][y]);
                            return;
                        } else {
                            if (!obj[areaNumber]) {
                                console.log('之前没areaNumber')
                                obj[areaNumber] = 0;
                            }
                            console.error('set areaNumber');
                            grid[x][y] = areaNumber;
                            visited[x][y] = 1;
                            obj[areaNumber] += 1;
                            console.log('obj', obj);
                        }
                        /**
                        二维数组数组坐标系
                        ————————————————y
                        |
                        |
                        |
                        |
                        x
                         */
                        dfs(x - 1, y);
                        dfs(x, y + 1);
                        dfs(x + 1, y);
                        dfs(x, y - 1);
                        // areaNumber++;
                    }
                    for (let i = 0; i < rowCount; i++) {
                        for (let j = 0; j < colCount; j++) {
                            if (grid[i][j] === 1) {//岛屿部分
                                dfs(i, j);//i,j为查找起点
                                areaNumber++;//每个查找起点对应一个areaNumber
                            }
                        }
                    }
                    let max = 0;
                    for (let index in obj) {
                        max = Math.max(obj[index], max);
                    }
                    return max;
                }
            
            }

        })
    </script>
</body>

</html>